package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.shaded.com.google.gson.JsonObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.core.util.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;

	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;


	@Override
	public List<JSONObject> getBaseCategoryList() {

		List<JSONObject> list = new ArrayList<>();

		List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList(null);

		Map<Long, List<BaseCategoryView>> categoryMap = baseCategoryViews.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));

		Set<Map.Entry<Long, List<BaseCategoryView>>> entries = categoryMap.entrySet();

		// 获取迭代器
		Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator = entries.iterator();
		while (iterator.hasNext()) {
			Map.Entry<Long, List<BaseCategoryView>> entry = iterator.next();
			Long category1Id = entry.getKey();
			String category1Name = entry.getValue().get(0).getCategory1Name();
			// 创建一个Category1 对象
			//BaseCategory1 baseCategory1 = new BaseCategory1();
			JSONObject category1 = new JSONObject();

			category1.put("categoryName", category1Name);
			category1.put("categoryId", category1Id);

			//List<JSONObject> category2List = new ArrayList<>();

			List<BaseCategoryView> baseCategoryViews1 = entry.getValue();

			// 创建一个二级分类集合
			List<JSONObject> category2List = new ArrayList<>();

			Map<Long, List<BaseCategoryView>> categoryMap2 = baseCategoryViews1.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			Set<Map.Entry<Long, List<BaseCategoryView>>> entries2 = categoryMap2.entrySet();
			Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator1 = entries2.iterator();
			while (iterator1.hasNext()) {
				Map.Entry<Long, List<BaseCategoryView>> entry2 = iterator1.next();
				Long category2Id = entry2.getKey();
				List<BaseCategoryView> baseCategoryViews2 = entry2.getValue();
				String category2Name = baseCategoryViews2.get(0).getCategory2Name();

				// 创建一个Category2 对象
				JSONObject category2 = new JSONObject();
				category2.put("categoryName", category2Name);
				category2.put("categoryId", category2Id);
				
				// 获取三级分类集合
				// 创建三级分类集合
				List<JSONObject> category3List = new ArrayList<>();
				baseCategoryViews2.forEach(baseCategoryView -> {
					JSONObject category3 = new JSONObject();
					category3.put("categoryName", baseCategoryView.getCategory3Name());
					category3.put("categoryId", baseCategoryView.getCategory3Id());
					category3List.add(category3);
				});

//				List<JSONObject> category3List = baseCategoryViews2.stream().map(baseCategoryView -> {
//					JSONObject category3 = new JSONObject();
//					category3.put("categoryName", baseCategoryView.getCategory3Name());
//					category3.put("categoryId", baseCategoryView.getCategory3Id());
//					return category3;
//				}).collect(Collectors.toList());

				category2.put("categoryChild",category3List);
				category2List.add(category2);
			}

			category1.put("categoryChild",category2List);
			list.add(category1);
		}
		return list;
	}
}
